package org.erepublik.company.controllers;

import javax.servlet.http.HttpServletRequest;

import org.erepublik.company.Location;
import org.erepublik.company.charts.AbstractJSONDataCollector;
import org.erepublik.company.charts.ChartDataSet;
import org.erepublik.company.searchCriteria.EFoodSearchCriteria;
import org.erepublik.company.searchCriteria.EHouseChartCriteria;
import org.erepublik.company.searchCriteria.EMovingDistance;
import org.erepublik.company.searchCriteria.EWeaponChartCriteria;
import org.erepublik.company.utils.Utils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
@RequestMapping("/priceHistoryChart.html")
public class PriceHistoryChartController extends AbstractController {


    @SuppressWarnings("unchecked")
    @RequestMapping(method = RequestMethod.GET)
    public String showForm(HttpServletRequest request, ModelMap model) {
        fillModelMap(model);
        
        AbstractJSONDataCollector provider = countryDataService.getDataCollctor(request.getParameter("productType"));
        Location location = null;
        Enum atribute = null;
        System.out.println("parametr to "+request.getParameter("country"));
        location = Utils.getLocationFromString( request.getParameter("country") );
        
        if (request.getParameter("atribute") !=null) {

           if (request.getParameter("productType").equals("house"))
                atribute = EHouseChartCriteria.valueOf( request.getParameter("atribute") );
           if (request.getParameter("productType").equals("ticket"))
               atribute = EMovingDistance.valueOf( request.getParameter("atribute") );
           
           if (request.getParameter("productType").equals("tank") ||
                   request.getParameter("productType").equals("rifle") ||
                   request.getParameter("productType").equals("artillery") || 
                   request.getParameter("productType").equals("helicopter"))
               atribute = EWeaponChartCriteria.valueOf( request.getParameter("atribute") );
        }
        if (request.getParameter("productType").equals("food"))
            atribute = EFoodSearchCriteria.WELLNESS;
        
        ChartDataSet chartData = provider.getData(location, atribute.toString());
 
        String countryName = location.getName();

        model.addAttribute("chart24h", chartData.getGoogleChartFor24hours(atribute,countryName));
        model.addAttribute("chart7d", chartData.getGoogleChartFor7days(atribute,countryName));
        model.addAttribute("chart8w", chartData.getGoogleChartFor2months(atribute, countryName));
        model.addAttribute("productType", request.getParameter("productType"));
        
        return "priceHistoryChart";
    }
}
